<?php
/**
 * Created by Vscode.
 * User: xiayu
 * Date: 2022/11/10
 * Time: 9:47
 */

namespace app\listener\crmeb;

use app\model\TestLog;
use app\model\WaterEleRecord;
use app\model\PayRecord;
use app\model\Room;
use app\model\WaterElePrice;
use app\logic\PayRecordLogic;

class TestCrmeb
{
    /**
     * 测试定时任务
     * @param $data
     */
    public function handle($data)
    {
        //   $res = TestLog::addTestLog(123456);
      $payRecord = new PayRecord;
      $room = new Room;
      $WaterEle = new WaterElePrice;
      $payRecordLogic = new PayRecordLogic;
      $where['state'] = 0;
      $list = WaterEleRecord::where($where)->order('id','desc')->select();
    //   $monthArr = [];
      foreach ($list as $key => $value) {
            $month = date("Y-m", $value['record_date']);

            //指定月份第一天的时间戳
            $lastMonthfirstDay = strtotime(date("Y-m-d 00:0:00", strtotime("first day of {$month}")));

            // 指定月份最后一天的时间戳
            $lastMonthLastDay = strtotime(date("Y-m-d 23:59:59", strtotime("{$month} +1 month -1 day")));
            $findWhere = [];
            $findWhere[]   = ['fee_date', 'between', [$lastMonthfirstDay,$lastMonthLastDay], 'AND'];
            $findWhere[]   = ['room_id', '=', $value['room_id']];

            // 获取房间该月是否已生成缴费记录
            $findRecord = $payRecord->getPayRecord($findWhere);
            if ($findRecord) {
                // echo '我进来了';
                // throw new MessageException('该房间' . $month . '已生成缴费记录');
                $upadteWhere['id'] = $value['id'];
                $updatedata = [
                    'state' =>2
                ];
                WaterEleRecord::where($upadteWhere)->update($updatedata);
            }else{
                // 获取房间的租金
                $roomPriceWhere['id'] = $value['room_id'];
                $roomPrice = $room->getRoomPrice($roomPriceWhere);

                // 获取房间的物业费
                $propertyPrice = $room->getPropertyPrice($roomPriceWhere);

                $params['m_water'] =  $value['water'] - $value['before_water'];
                $params['m_electricity']   =   $value['electricity'] - $value['before_electricity'];

                $waterPrice = $WaterEle->getWaterElePrice(0);
                $elePrice   = $WaterEle->getWaterElePrice(1);

                $waterPrice = $waterPrice->toArray();
                $elePrice = $elePrice->toArray();
                // 初始化水费值
                $params['water_price'] = 0;
                $m_water  = $params['m_water'];

                foreach ($waterPrice as $key => $waterValue) {
                    // if($key == 0){
                    if (preg_match("/^[1-9][0-9]*$/", ($m_water - $waterValue['top']))) {
                        $params['water_price'] = $params['water_price'] + ($waterValue['top'] - $waterValue['bottom']) * $waterValue['unit_price'];
                    } else {
                        if (preg_match("/^[1-9][0-9]*$/", ($m_water - $waterValue['bottom']))) {
                            $params['water_price'] = $params['water_price'] + ($m_water - $waterValue['bottom']) * $waterValue['unit_price'];
                        }
                    }
                }
                //初始化电费值
                $params['electricity_price'] = 0;
                $m_ele  = $params['m_electricity'];

                foreach ($elePrice as $key => $eleValue) {
                    // if($key == 0){
                    if (preg_match("/^[1-9][0-9]*$/",
                        ($m_ele - $eleValue['top'])
                    )) {
                        $params['electricity_price'] = $params['electricity_price'] + ($eleValue['top'] - $eleValue['bottom']) * $eleValue['unit_price'];
                    } else {
                        if (preg_match("/^[1-9][0-9]*$/", ($m_ele - $eleValue['bottom']))) {
                            $params['electricity_price'] = $params['electricity_price'] + ($m_ele - $eleValue['bottom']) * $eleValue['unit_price'];
                        }
                    }
                }

                //共计金额
                $params['fee']              = $roomPrice + $params['water_price'] + $params['electricity_price'] + $propertyPrice;
                $params['room_price']       = $roomPrice;
                $params['property_price']   = $propertyPrice;
                $params['room_id']          = $value['room_id'];
                $params['fee_date']         = $value['record_date'];
                $params['water']            = $value['water'];
                $params['electricity']      = $value['electricity'];
                $params['mark']             = '';
                $params['status']           = 0;
                $response = PayRecord::create($params);

                // 将水电记录修改为已使用
                if($response){
                    WaterEleRecord::where('id', '=', $value['id'])->update(['state' => 1]);
                    // return $response;
                    $res  = $payRecordLogic->addLesseeRecord($response, $params);
                }
                
            }

      }
      
    }
}